package com.rf.richfitwheel.common.utils;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.*;


/**
 * 分页工具类
 * 
 * @author hushouquan
 * @date 2018-11-27
 */
public class PageUtils implements Serializable {

	private static final long serialVersionUID = -8682614396033424153L;
	//总记录数
	private int totalCount;
	//每页记录数
	private int pageSize;
	//总页数
	private int totalPage;
	//当前页数
	private int currPage;
	//列表数据
	private List<?> list;
	//表头
	private List<TableHeadBo> headList;
	// 列表数据2，有的返回属性是bodyList
	private List<?> bodyList;

	//
	private int page = 1; // 默认第一页

	private int limit = 10; //默认每页10条

	public PageUtils(){
	}

//	/**
//	 * 分页
//	 * @param list        列表数据
//	 * @param totalCount  总记录数
//	 * @param pageSize    每页记录数
//	 * @param currPage    当前页数
//	 */
//	public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
//		this.list = list;
//		this.totalCount = totalCount;
//		this.pageSize = pageSize;
//		this.currPage = currPage;
//		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
//	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = Math.min(pageSize, 200);
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}
	
	/**
	 * 分页最大限制500
	 * @param limit 每页条数
	 */
	public void setLimit(int limit) {
		this.limit = Math.min(limit, 500);
	}


	public List<TableHeadBo> getHeadList() {
		return headList;
	}

	public void setHeadList(List<TableHeadBo> headList) {
		this.headList = headList;
	}

	public List<?> getBodyList() {
		return bodyList;
	}

	public void setBodyList(List<?> bodyList) {
		this.bodyList = bodyList;
	}

	/**
	 * 分页
	 */
	public PageUtils(Page<?> page) {
		this.list = page.getResult();
		this.totalCount = (int)page.getTotal();
		//this.pageSize = page.getPageSize();
		//this.currPage = page.getStartRow();
		//this.totalPage = (int)page.getPages();
	}

	/**
	 * 分页
	 */
	public PageUtils(Page<?> page , int pages,int limit) {
		this.list = page.getResult();
		this.totalCount = (int)page.getTotal();
		this.page = pages;
		this.limit = limit;
		//this.pageSize = page.getPageSize();
		//this.currPage = page.getStartRow();
		//this.totalPage = (int)page.getPages();
	}


	public PageUtils(List<?> list, int total) {
		this.list = list;
		this.totalCount = total;
	}

	public static PageUtils getPageInfo(Map<String, Object> params){
		PageUtils page = new PageUtils();
		if(params!=null&&params.containsKey("page")&&params.containsKey("limit")&&params.get("page")!=null&&params.get("limit")!=null){
			page.setPage(Integer.parseInt(String.valueOf(params.get("page"))));
			page.setLimit(Integer.parseInt(String.valueOf(params.get("limit"))));
		}
			return page;

		}

	public static PageUtils getPageInfo(PageParameter<?> pageParameter){
		PageUtils page =new PageUtils();
		if(pageParameter != null && pageParameter.getPage() != null && pageParameter.getLimit() != null){
			page.setPage(pageParameter.getPage());
			page.setLimit(pageParameter.getLimit());
			return page;
		}else{
			return page;
		}
	}


	public static PageUtils getPageTable(List<?> bodyList, List<String> names, List<String> propList, PageInfo<?> pageInfo) {
		Objects.requireNonNull(names, "入参表头名称不能为空");
		Objects.requireNonNull(propList, "入参表头映射存储对象不能为空");
		Objects.requireNonNull(pageInfo, "入参pageInfo不能为空");

		final List<TableHeadBo> headList = new ArrayList<>();

		names.forEach(name -> {
			int index = names.indexOf(name);
			final TableHeadBo bo = new TableHeadBo();
			bo.setLabel(name);
			bo.setProp(propList.get(index));
			headList.add(bo);
		});

		PageUtils page = new PageUtils();

		//当前页
		page.setPage(pageInfo.getPageNum());
		//当前页数
		page.setCurrPage(pageInfo.getPageNum());
		//每页的记录数
		page.setLimit(pageInfo.getPageSize());
		//总的记录数
		page.setTotalCount((int) pageInfo.getTotal());
		//总页数
		page.setTotalPage(pageInfo.getPages());
		//当前页的记录数
		page.setPageSize(pageInfo.getSize());
		page.headList = headList;
		page.bodyList = bodyList;

		return page;
}

	/**
	 * 对非数据库查询结果集进行手动分页
	 * @param arrayList List
	 * @param pageNum Integer
	 * @param pageSize Integer
	 * @param <T> T
	 * @return
	 */
	public static <T> PageInfo<T> fromPlainList(List<T> arrayList, Integer pageNum, Integer pageSize) {
		//实现list分页
		PageHelper.startPage(pageNum, pageSize);
		int pageStart = pageNum == 1 || (arrayList.size() <= (pageNum - 1) * pageSize) ? 0 : (pageNum - 1) * pageSize;
		int pageEnd = Math.min(arrayList.size(), pageSize * pageNum);
		List<T> pageResult = new LinkedList<T>();
		if (arrayList.size() > pageStart) {
			pageResult = arrayList.subList(pageStart, pageEnd);
		}
		PageInfo<T> pageInfo = new PageInfo<T>(pageResult);
		//获取PageInfo其他参数
		pageInfo.setTotal(arrayList.size());
		int endRow = pageInfo.getEndRow() == 0 ? 0 : (pageNum - 1) * pageSize + pageInfo.getEndRow() + 1;
		pageInfo.setEndRow(endRow);
		boolean hasNextPage = arrayList.size() > pageSize * pageNum;
		pageInfo.setHasNextPage(hasNextPage);
		boolean hasPreviousPage = pageNum != 1;
		pageInfo.setHasPreviousPage(hasPreviousPage);
		pageInfo.setIsFirstPage(!hasPreviousPage);
		boolean isLastPage = arrayList.size() <= pageSize * pageNum ||
				arrayList.size() > (pageNum - 1) * pageSize && arrayList.size() <= pageSize * pageNum;
		pageInfo.setIsLastPage(isLastPage);
		int pages = arrayList.size() % pageSize == 0 ? arrayList.size() / pageSize : (arrayList.size() / pageSize) + 1;
		pageInfo.setNavigateLastPage(pages);
		int[] navigatePageNums = new int[pages];
		for (int i = 1; i < pages; i++) {
			navigatePageNums[i - 1] = i;
		}
		pageInfo.setNavigatepageNums(navigatePageNums);
		int nextPage = pageNum < pages ? pageNum + 1 : 0;
		pageInfo.setNextPage(nextPage);
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		pageInfo.setPages(pages);
		pageInfo.setPrePage(pageNum - 1);
		pageInfo.setSize(pageInfo.getList().size());
		int starRow = arrayList.size() < pageSize * pageNum ? 1 + pageSize * (pageNum - 1) : 0;
		pageInfo.setStartRow(starRow);
		return pageInfo;
	}
}
